﻿
Public Class pizzaClient
    Inherits System.Web.UI.Page

    Dim objpizzaPxy As New pizzaPxy.pizzaService
    Dim ordLine As pizzaPxy.orderLine
    Dim ds As New DataSet
    Dim sql As String
    Dim OrderLines As New ArrayList
    Dim DisplayFlag_OneChecked As Boolean = False
    Dim DisplayFlag_GoodData As Boolean = True
    'Dim objDV As New DataView(ds.Tables(0))

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            'sql = "SELECT * FROM PIZZA"
            gvPizza.Visible = True
            lblAddr.Visible = True
            lblName.Visible = True
            lblBadValidation.Visible = False
            ds = objpizzaPxy.GetPizzaTable()
            Dim objDV As New DataView(ds.Tables(0))
            objDV.Sort = "pizzaType"
            gvPizza.DataSource = objDV
            gvPizza.DataBind()
        End If
    End Sub

    'DISPLAY ORDER summary in gridview
    Private Sub showOrder()
        gvOrder.DataSource = OrderLines
        gvOrder.DataBind()
        gvPizza.Visible = False
        gvOrder.Visible = True
        lblBadValidation.Visible = False
    End Sub


    'Submit an order, yo
    Protected Sub btnSubmitOrder_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmitOrder.Click

        Dim i As Integer
        Dim Cbox As CheckBox
        Dim Tbox As TextBox
        Dim dd As DropDownList

        'objDB = Session("db")
        'Dim objFunc As New pizzaFunctions(objDB)

        gvPizza.Visible = False
        txtName.Visible = False
        txtAddr.Visible = False
        lblName.Visible = False
        lblAddr.Visible = False
        radBtnDeliveryType.Visible = False


        lblDispName.Text = txtName.Text
        lblDispAddr.Text = txtAddr.Text
        lblDispDelivery.Text = radBtnDeliveryType.SelectedValue.ToString
        lblDispAddr.Visible = True
        lblDispName.Visible = True
        lblDispDelivery.Visible = True
        lblOrderInfo.Visible = True

        DisplayFlag_GoodData = True
        DisplayFlag_OneChecked = False

        'check input gridview to see what pizza customers want
        For i = 0 To (gvPizza.Rows.Count - 1)
            Cbox = CType(gvPizza.Rows(i).FindControl("chkSelectPizza"), CheckBox)
            If Cbox.Checked Then
                'Dim objOrder As New orderLine
                ordLine = New pizzaPxy.orderLine

                DisplayFlag_OneChecked = True
                Tbox = CType(gvPizza.Rows(i).FindControl("txtQtyOrd"), TextBox)

                If txtName.Text = "" Then
                    DisplayFlag_GoodData = False
                    lblBadValidation.Visible = True
                    lblBadValidation.Text = "Please ensure that the name box is not empty. OR ELSE."
                    'MsgBox("Please ensure that the name box is not empty. OR ELSE.")
                    gvPizza.Visible = True
                    txtName.Visible = True
                    txtAddr.Visible = True
                    lblName.Visible = True
                    lblAddr.Visible = True
                    lblOrderInfo.Visible = False
                    lblDispAddr.Visible = False
                    lblDispDelivery.Visible = False
                    lblDispName.Visible = False
                    radBtnDeliveryType.Visible = True
                    Exit Sub
                End If

                If txtAddr.Text = "" Then
                    DisplayFlag_GoodData = False
                    lblBadValidation.Visible = True
                    lblBadValidation.Text = "Please ensure that the address box is not empty. OR ELSE"
                    ' MsgBox("Please ensure that the address box is not empty. OR ELSE.")
                    txtName.Visible = True
                    txtAddr.Visible = True
                    gvPizza.Visible = True
                    lblName.Visible = True
                    lblAddr.Visible = True
                    lblOrderInfo.Visible = False
                    lblDispAddr.Visible = False
                    lblDispDelivery.Visible = False
                    lblDispName.Visible = False
                    radBtnDeliveryType.Visible = True
                    Exit Sub
                End If


                If Not IsNumeric(Tbox.Text) And Not IsNothing(Tbox.Text) Then
                    DisplayFlag_GoodData = False
                    lblBadValidation.Visible = True
                    lblBadValidation.Text = "Only Integers should be present in Quantity fields"
                    ' MsgBox("Only Integers should be present in Quantity fields")
                    gvPizza.Visible = True
                    txtName.Visible = True
                    txtAddr.Visible = True
                    lblAddr.Visible = True
                    lblName.Visible = True
                    lblOrderInfo.Visible = False
                    lblDispAddr.Visible = False
                    lblDispDelivery.Visible = False
                    lblDispName.Visible = False
                    radBtnDeliveryType.Visible = True
                    Exit Sub
                Else

                    dd = CType(gvPizza.Rows(i).FindControl("pizzaSizeDropDown"), DropDownList)
                    ordLine.pizzaType = gvPizza.Rows(i).Cells(1).Text
                    ordLine.pieSize = dd.SelectedValue
                    ordLine.quantity = Tbox.Text
                    'MsgBox(ordLine.pizzaType.ToString & " " & ordLine.pieSize.ToString & " " & ordLine.quantity.ToString)

                    'something with this area is causing the output gridview to not display correctly
                    'OrderLines.Add(ordLine)
                    objpizzaPxy.GetPizzaInfo(ordLine)
                    'MsgBox(ordLine.pizzaType.ToString & " " & ordLine.pieSize.ToString & " " & ordLine.quantity.ToString)
                    OrderLines.Add(ordLine)
                    'MsgBox(OrderLines.Count)

                End If
            End If
        Next
        showOrder()
        'calculate orderline totals and display gridview
        If DisplayFlag_OneChecked And DisplayFlag_GoodData Then
            Dim totalQty As Integer
            Dim totalOrderCost As Decimal

            '  gvOrder.DataSource = OrderLines
            ' gvOrder.DataBind()

            For x As Integer = 0 To (gvOrder.Rows.Count - 1)

                totalOrderCost += CType(gvOrder.Rows(x).Cells(4).Text, Decimal)
                totalQty += CType(gvOrder.Rows(x).Cells(2).Text, Integer)

            Next

            gvOrder.Columns(0).FooterText = "Totals"
            gvOrder.Columns(2).FooterText = totalQty.ToString
            gvOrder.Columns(4).FooterText = totalOrderCost.ToString

            showOrder()
            lblBadValidation.Visible = False

        ElseIf Not DisplayFlag_OneChecked Then
            lblBadValidation.Visible = True
            lblBadValidation.Text = "Please select at least one kind of pizza to be ordered"
            ' MsgBox("Please select at least one kind of pizza to be ordered")
            gvPizza.Visible = True
            lblName.Visible = True
            lblAddr.Visible = True
            lblOrderInfo.Visible = False
            lblDispAddr.Visible = False
            lblDispDelivery.Visible = False
            lblDispName.Visible = False
            radBtnDeliveryType.Visible = True
            txtAddr.Visible = True
            txtName.Visible = True
            radBtnDeliveryType.Visible = True
            Exit Sub
        Else
            lblBadValidation.Visible = True
            lblBadValidation.Text = "Quantity field must be an integer"
            ' MsgBox("Quantity field must be an integer")
            gvPizza.Visible = True
            lblName.Visible = True
            lblAddr.Visible = True
            lblOrderInfo.Visible = False
            lblDispAddr.Visible = False
            lblDispDelivery.Visible = False
            lblDispName.Visible = False
            radBtnDeliveryType.Visible = True
            Exit Sub
        End If

    End Sub

    'SHOW MGMT REPORT
    Protected Sub mgmtReport_Click(sender As Object, e As EventArgs) Handles mgmtReport.Click
        Dim repSql As String = "SELECT * FROM PIZZA ORDER BY TotalSales"
        Dim objDV As DataView
        gvPizza.Visible = False
        gvOrder.Visible = False
        lblBadValidation.Visible = False
        gvReport.Visible = True

        ds = objpizzaPxy.GetPizzaTable()
        Dim objMgmtDV As New DataView(ds.Tables(0))
        objMgmtDV.Sort = "TotalSales"

        gvReport.DataSource = objMgmtDV
        gvReport.DataBind()

        gvOrder.Columns(0).FooterText = "Totals"

    End Sub

    'RESET ORDER FORM
    Protected Sub btnResetOrder_Click(sender As Object, e As EventArgs) Handles btnResetOrder.Click
        gvOrder.Visible = False
        gvPizza.Visible = True
        gvReport.Visible = False
        txtName.Visible = True
        txtAddr.Visible = True
        lblName.Visible = True
        lblAddr.Visible = True
        radBtnDeliveryType.Visible = False
        lblDispAddr.Visible = False
        lblDispDelivery.Visible = False
        lblDispName.Visible = False
        lblOrderInfo.Visible = False
        radBtnDeliveryType.Visible = True
        txtName.Text = ""
        txtAddr.Text = ""
        lblBadValidation.Visible = False
        'gvPizza.Columns.Clear()

    End Sub
End Class